import 'package:flutter_axrjc/datasource/network/api/LightApi.dart';
import 'package:flutter_axrjc/datasource/network/api/MomentApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/NoticeBean.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'state.dart';

class NoticeSettingLogic extends GetxController {
  final NoticeSettingState state = NoticeSettingState();
  var page = 1;
  var refreshController = RefreshController(initialRefresh: true);
  Future<void> getNotice({required bool isRefresh}) async {
    if (isRefresh) {
      page = 1;
      state.list.clear();
    } else {
      page++;
    }
    try {
      var res = await MomentApi.getNotice(
          remindType: state.remindType.value, page: page);
      for (var element in res) {
        element.checked.value = "${element.remindStatus}" == "0";
      }
      state.list.addAll(res);
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      EasyLoading.showToast("$e");
    }
    if (isRefresh) {
      refreshController.refreshCompleted();
    } else {
      refreshController.loadComplete();
    }
  }

  Future<void> setStatus(NoticeBean bean, bool v) async {
    try {
      await LightApi.setStatus("${bean.remindId}", v ? "0" : "1");
      bean.checked.value = v;
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      EasyLoading.showToast("$e");
    }
  }

  Future<void> deleteNotice(String s) async {
    await LightApi.deleteNotice(key: s);
    getNotice(isRefresh: true);
  }
}
